import resources.Constants;

import registnet.dao.FinalSectionPeer;

FinalSectionPeer finalSectionPeer = new FinalSectionPeer();


List data = new Vector();
List resultSet = new Vector();

boolean isShowingInfo;
boolean isSearching;

/* [start] 	initializes gui variables */
String _instance = bundle.getProperty("registnet.module.data_management.unit");
String _instances = "Final Sections";

String _proceed = bundle.getProperty("registnet.messages.proceed");
String _check = bundle.getProperty("registnet.messages.check");
String _successful = bundle.getProperty("registnet.messages.successful");
String _failed = bundle.getProperty("registnet.messages.failed");
String _select_one = bundle.getProperty("registnet.messages.select_one");
String _selected = bundle.getProperty("registnet.messages.selected");
String _use_search_result_viewer = bundle.getProperty("registnet.messages.use_search_result_viewer");
final int LECTURE_COUNT = 0;
final int LAB_COUNT = 1;
final int STUDENT_COUNT = 2;

/* [end] */



/* [start]	prepares the tree */
ArrayList parentList = new ArrayList();
Map mapSTN_parent = new HashMap();
Map mapAL_parent = new HashMap();
Map mapAL_child = new HashMap();
Map map_treeitem = new HashMap();
SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
/* [end] */



/* [start]	defines the renderer for the tree */
public class MyTreeitemRenderer implements TreeitemRenderer {
	public void render(Treeitem item, Object data) throws Exception {
		String str = data.toString();
		Treerow tr = null;
		Treecell tc = new Treecell(str);
		if(item.getTreerow()==null){
			tr = new Treerow();
			tr.setParent(item);
			map_treeitem.put(str, item);
		}else{
			tr = item.getTreerow(); 
			tr.getChildren().clear();
		}
		tc.setParent(tr);
	}
}
/* [end] */



/* [start]	retrieves the units and places them on the tree;
			limits the courses to retrieve depending on the user type */
public void retrieveObjects(){
	map_treeitem.clear();
	mapSTN_parent.clear();
	mapAL_parent.clear();
	mapAL_child.clear();
	parentList.clear();
	List courses = new Vector();
	int unitId = (new registnet.dao.UnitPeer()).getUnitId(_office);
	courses = (new registnet.dao.CoursePeer()).getCoursesByUnit(unitId);
	for(int i=0; i<courses.size(); i++){
		ArrayList alChild = new ArrayList();
		ArrayList alLeaf = new ArrayList();
		SimpleTreeNode stnParent = new SimpleTreeNode(courses.get(i).getCourseCode(), alChild);
		mapSTN_parent.put(courses.get(i).getCourseCode(), stnParent);
		mapAL_parent.put(courses.get(i).getCourseCode(), alChild);
		mapAL_child.put(courses.get(i).getCourseCode(), alLeaf);
		parentList.add(stnParent);
	}
	SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
	SimpleTreeModel stm = new SimpleTreeModel(root);
	instance_tree.setModel(stm);
	instance_tree.setTreeitemRenderer(new MyTreeitemRenderer());
}
/* [end] */



/* [start]	shows all the entries */
void showAll(){
	retrieveObjects();
	try{
		instance_tree.setSelectedItem(map_treeitem.get(parentList.get(0).getData()));
	}catch(Exception e){}
	try{
		title.setValue("");
		contents.removeChild(contents.getFirstChild());
	}catch(Exception e ){}
}
/* [end] */



/* [start]	defines an event listener for the items on the tree;
			when the item is selected, its info is displayed */
private class SelectEventListener implements EventListener {
	public SelectEventListener (){}
	public void onEvent(Event event) throws UiException {
		showInfo();
	}
}	
/* [end] */


/* [start] displays the info of the selected item */
void showInfo(){
	isShowingInfo = true;
	isSearching = false;
	if (contents.getFirstChild() != null){
		contents.removeChild(contents.getFirstChild());
	}
	try{
		if (instance_tree.getSelectedCount() == 1){
			
			Component show_finalsection_form = Executions.createComponents("/WEB-INF/macros/data_management/finalsection_form.zul", contents, null);
			int[] stat = getStatistics(show_finalsection_form.getFellow("rows"));
				if(stat!=null){
					title.setValue(instance_tree.getSelectedItem().getLabel());
					show_finalsection_form.getFellow("lecture_count").setValue(stat[LECTURE_COUNT]+"");
					show_finalsection_form.getFellow("lab_count").setValue(stat[LAB_COUNT]+"");
					show_finalsection_form.getFellow("student_count").setValue(stat[STUDENT_COUNT]+"");
					show_finalsection_form.getFellow("section_grid").setVisible(true);
					show_finalsection_form.getFellow("statistics").setVisible(true);
				}
				else{
					show_finalsection_form.detach();
					alert("Not finalized yet.");
					title.setValue("");
				}
		}
		else if (instance_tree.getSelectedCount() > 1)
			title.setValue("" + instance_tree.getSelectedCount() + " " + _instance + " " + _selected);
		else
			title.setValue("");
	}catch (Exception e) { title.setValue(""); }
}
/* [end] */

int[] getStatistics(Rows rows){
	int courseId = (new registnet.dao.CoursePeer()).getCourseId(instance_tree.getSelectedItem().getLabel()); 
	int[] stat = new int[3];
	stat[0]=0;
	stat[1]=0;
	stat[2]=0;
	List lectures = (new registnet.dao.FinalSectionPeer()).getLectureFinalSections(courseId,1,"2008-2009");
	if(lectures.size() == 0){
		return null;
	}
	for(int i=0; i<lectures.size(); i++){
		int lecStudentCount = 0;
		lecStudentCount = lectures.get(i).getStudentCount();
		
		Group lec = new Group();
		lec.setLabel(lectures.get(i).getFinalSectionName()+"   [" + lecStudentCount + "]");
		lec.setParent(rows);
		
		List labs = (new registnet.dao.FinalSectionPeer()).getLaboratory(courseId, lectures.get(i).getFinalSectionName(),1,"2008-2009");
		int labSize = labs.size();
		if(labSize == 0)
			lec.setHeight("2px");
		for(int j=0; j<labSize; j++){
			Row row = new Row();
			Label lab = new Label();
			int labStudentCount = 0;
			labStudentCount = labs.get(j).getStudentCount();
			lab.setValue(labs.get(j).getFinalSectionName()+"   [" + labStudentCount + "]");
			lab.setParent(row);
			row.setParent(rows);
		}
		
		
		try{	
			stat[LAB_COUNT] += labSize;
			stat[STUDENT_COUNT] += lecStudentCount;
		}
		catch(Exception e){}	
	}
	
	stat[LECTURE_COUNT] = lectures.size();
	return stat;
}